package com.neuedu.wemall.seller.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.wemall.common.vo.ResponseData;
import com.neuedu.wemall.dao.entity.Category;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.neuedu.wemall.service.ICategoryService;

/**
 * Author lu
 * Date   2025-09-14 21:16:19
 */
@Log4j2
@RequestMapping("/wemall/seller/category/api")
@RestController
@Tag(name = "商品分类控制器")
public class CategoryController {

    @Autowired
    @Qualifier("categoryService")
    private ICategoryService categoryService;

    /**
     * 新增商品分类
     *
     * @param category
     * @return
     */
    @PostMapping("/create")
    @Parameters(
            {@Parameter(name = "name", description = "分类名称", required = true), @Parameter(name = "desc", description = "分类描述", required = false), @Parameter(name = "parentId", description = "父分类ID", required = false)
            }
    )
    public ResponseData create(@Parameter(hidden = true)Category category) {
        if (category == null) {
            return ResponseData.missParam("分类名称");
        }
        if (StrUtil.isEmpty(category.getName())) {
            return ResponseData.missParam("分类名称");
        }
        int rows = categoryService.insertOne(category);
        if (2 == rows) {
            return ResponseData.createFail("商品分类名称已存在");
        }
        return ResponseData.success(rows);
    }

    /**
     * 更新商品分类
     *
     * @param category
     * @return
     */
    @PostMapping("/modify")
    @Operation(summary = "更新商品分类")
    @Parameters({
            @Parameter(name = "id", description = "分类ID", required = true),
            @Parameter(name = "name", description = "分类名称", required = false),
            @Parameter(name = "desc", description = "分类描述", required = false),
            @Parameter(name = "parentId", description = "父分类ID", required = false),
            @Parameter(name = "sort", description = "排序索引", required = false),
            @Parameter(name = "level", description = "分类级别", required = false),
            @Parameter(name = "delStatus", description = "删除状态", required = false)
    })
    public ResponseData modify(@Parameter(hidden = true) Category category) {
        if (category == null) {
            return ResponseData.modifyFail("缺少参数");
        }
        if (category.getId() == null) {
            return ResponseData.missParam("分类ID");
        }
        int rows = categoryService.updateOneById(category);
        if (2 == rows) {
            return ResponseData.modifyFail("商品分类名称重复");
        }
        return ResponseData.success(rows);
    }

    /**
     * 删除商品分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据ID删除商品分类")
    public ResponseData delete(@PathVariable("id") @Parameter(name = "id", description = "分类ID", required = true) Integer id) {
        boolean result = categoryService.removeById(id);
        return ResponseData.success(result);
    }

    /**
     * 查询商品分类列表
     *
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询商品分类分页查询")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页码", required = false),
            @Parameter(name = "pageSize", description = "每页记录数", required = false),
            @Parameter(name = "keyword", description = "搜索关键词", required = false),
            @Parameter(name = "level", description = "分类级别(1:一级;2:二级)", required = false),
            @Parameter(name = "delStatus", description = "删除状态(1:未删除;9:已删除)", required = false)
    })
    public ResponseData getPageList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(value = "keyword",required = false) String keyword,
                                    @Parameter(hidden = true) Category category) {
        Page<Category> page = categoryService.getPageList(pageNum, pageSize, keyword, category);
        return ResponseData.success(page);
    }

    /**
     * 根据ID查询商品分类
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询商品分类")
    public ResponseData get(@PathVariable("id") @Parameter(name = "id", description = "分类ID", required = true) Integer id) {
        Category category = categoryService.getById(id);
        if (category == null) {
            return ResponseData.fail(100400,"商品分类不存在");
        }
        Integer parentId = category.getParentId();
        if (parentId != null) {
            Category parent = categoryService.getById(parentId);
            category.setParent(parent);
        }
        return ResponseData.success(category);
    }

}